package com.cinyi.cyds.util;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYBoxAnnotation;
import org.jfree.chart.annotations.XYTextAnnotation;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.TextAnchor;

/**
 * <JFreechart公共类> <功能详细描述>
 * 
 * @author zwq
 * @version [版本号, 2012-5-30]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class CreateJFreechartUtil {
	public String setImages(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		DefaultCategoryDataset dataset = getDataSet();
		JFreeChart chart = ChartFactory.createBarChart3D("水果产量图", // 图表标题
				"水果", // 目录轴的显示标签
				"产量", // 数值轴的显示标签
				dataset, // 数据集
				PlotOrientation.VERTICAL, // 图表方向：水平、垂直
				true, // 是否显示图例（对于简单的柱状图必须是false）
				false, // 是否生成工具
				false // 是否生成URL链接
				);
		// 解决乱码问题
		setImageFont(chart);

		// 生成图片
		String filename = ServletUtilities.saveChartAsPNG(chart, 600, 400,
				null, request.getSession());

		ServletContext servlet = request.getSession().getServletContext();

		// 得到功能路径
		String imagePath_area = servlet.getContextPath()
				+ "/servlet/DisplayChart?filename=" + filename;

		return imagePath_area;
	}

	/**
	 * 解决乱码问题
	 */
	private void setImageFont(JFreeChart chart) {
		CategoryPlot plot = (CategoryPlot) chart.getPlot();
		CategoryAxis domainAxis = plot.getDomainAxis();
		ValueAxis numberaxis = plot.getRangeAxis();

		// 设置标题文字
		chart.getTitle().setFont(new Font("宋体", Font.BOLD, 12));

		// 设置X轴坐标上的文字
		domainAxis.setTickLabelFont(new Font("黑体", Font.PLAIN, 11));

		// 设置X 轴的标题文字
		domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));

		// 设置Y 轴坐标上的文字
		numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));

		// 设置Y 轴的标题文字
		numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));

		// 设置底部文字
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
	}

	/**
	 * 获取一个组合数据集对象
	 * 
	 * @return
	 */
	private DefaultCategoryDataset getDataSet() {
		DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(100, "北京", "苹果");
		dataset.addValue(120, "上海", "苹果");
		dataset.addValue(380, "广州", "苹果");
		dataset.addValue(230, "北京", "梨子");
		dataset.addValue(200, "上海", "梨子");
		dataset.addValue(230, "广州", "梨子");
		dataset.addValue(400, "北京", "葡萄");
		dataset.addValue(300, "上海", "葡萄");
		dataset.addValue(500, "广州", "葡萄");
		return dataset;
	}

	private static void generatePieJPG(DefaultPieDataset data, String title,
			String destFile) throws IOException {
		// DefaultPieDataset data=new DefaultPieDataset();
		// // 数据初始化
		// // data.setValue("高中以下", 380);
		// // data.setValue("高中", 1620);
		// // data.setValue("大专", 6100);
		// // data.setValue("本科", 8310);
		// // data.setValue("硕士", 3520);
		// // data.setValue("博士", 80);
		//
		// data.setValue("高中以下", 0.1);
		// data.setValue("高中", 0.1);
		// data.setValue("大专", 0.1);
		// data.setValue("本科", 0.1);
		// data.setValue("硕士", 0.1);
		// data.setValue("博士", 5);

		PiePlot3D plot = new PiePlot3D(data);// 生成一个3D饼图
		JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT,
				plot, true);// 生成一个图
		chart.setBackgroundPaint(java.awt.Color.white);// 可选，设置图片背景色
		chart.setTitle(title);// 可选，设置图片标题
		// plot.setToolTipGenerator(new StandardPieToolTipGenerator());

		// 500是图片长度，300是图片高度
		// String filename =
		// ServletUtilities.saveChartAsPNG(chart,500,300,info,session);
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ChartUtilities.writeChartAsJPEG(byteArrayOutputStream, chart, 500, 300);
		FileOutputStream file = new FileOutputStream(destFile);
		file.write(byteArrayOutputStream.toByteArray());
		file.close();
	}

	private static void generateBarJPG(DefaultCategoryDataset dataset,
			String title, String xAxisLabel, String yAxisLabel, String destFile)
			throws IOException {
		// row keys...
		// final String series1="First";
		//
		// // column keys...
		// final String category1="Category 1";
		// final String category2="Category 2";
		// final String category3="Category 3";
		// final String category4="Category 4";
		// final String category5="Category 5";
		//
		// // create the dataset...
		// final DefaultCategoryDataset dataset=new DefaultCategoryDataset();
		//
		// dataset.addValue(1.0, series1, category1);
		// dataset.addValue(4.0, series1, category2);
		// dataset.addValue(3.0, series1, category3);
		// dataset.addValue(5.0, series1, category4);
		// dataset.addValue(5.0, series1, category5);

		final JFreeChart chart = ChartFactory.createBarChart(title, // chart
																	// title
				xAxisLabel, // domain axis label
				yAxisLabel, // range axis label
				dataset, // data
				PlotOrientation.VERTICAL, // orientation
				true, // include legend
				true, // tooltips?
				true // URLs?
				);

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ChartUtilities.writeChartAsJPEG(byteArrayOutputStream, chart, 500, 300);

		FileOutputStream file = new FileOutputStream(destFile);
		file.write(byteArrayOutputStream.toByteArray());
		file.close();
	}

	private static void genLineJPG(List<Double> data, String title,
			String bottomtitle, String xAxisLabel, String yAxisLabel,
			String destFile) throws IOException {
		// 数据源
		XYSeriesCollection seriesCollection = new XYSeriesCollection();
		XYSeries series1 = new XYSeries(bottomtitle);
		int size = data.size();
		for (int i = 1; i <= data.size(); i++) {
			// x, y 都是数字型
			series1.add(i, data.get(i));
		}

		seriesCollection.addSeries(series1);

		genLineJPG(seriesCollection, title, bottomtitle, xAxisLabel,
				yAxisLabel, destFile);
	}

	private static void genLineJPG(XYSeriesCollection seriesCollection,
			String ttile, String bottomtitle, String xAxisLabel,
			String yAxisLabel, String destFile) throws IOException {

		// // 数据源
		// XYSeriesCollection seriesCollection=new XYSeriesCollection();
		// XYSeries series1=new XYSeries(bottomtitle);
		// // x, y 都是数字型
		// series1.add(1, 7.25);
		// series1.add(2, 4.81);
		// series1.add(3, 3.69);
		// series1.add(4, 3.53);
		// series1.add(5, 2.95);
		// series1.add(6, 3.96);
		//
		// seriesCollection.addSeries(series1);

		/**
		 * String title, // 图标题 String xAxisLabel x 轴标题 String yAxisLabel, y 轴标题
		 * XYDataset dataset, 数据源 PlotOrientation orientation, 显示方向 boolean
		 * legend, 是否显示图例 boolean tooltips, 是否显示 tooltip boolean urls 是否指定 url )
		 **/
		JFreeChart chart = ChartFactory.createXYLineChart(ttile, xAxisLabel,
				yAxisLabel, seriesCollection, PlotOrientation.VERTICAL, true,
				true, false);

		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		ChartUtilities.writeChartAsJPEG(byteArrayOutputStream, chart, 500, 300);
		FileOutputStream file = new FileOutputStream(destFile);
		file.write(byteArrayOutputStream.toByteArray());
		file.close();
	}

	public static String createPieChart3D(String title, PieDataset dataset,
			String path) {
		JFreeChart chart = ChartFactory.createPieChart3D(title, dataset, true,
				true, Locale.CHINA);

		// 使下面的说明标签字体清晰,类似于去锯齿的效果
		chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
				RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
		chart.setTextAntiAlias(false);
		// 图片背景色
		chart.setBackgroundPaint(Color.white);

		// 设置图标题的字体重新设置title(否组有些版本Title会出现乱码)
		chart.getTitle().setFont((new Font("隶书", Font.CENTER_BASELINE, 20)));

		// 设置图例(Legend)上的文字(//底部的字体)
		chart.getLegend().setItemFont(new Font("隶书", Font.CENTER_BASELINE, 15));

		PiePlot3D plot = (PiePlot3D) chart.getPlot();
		// 图片中显示百分比:默认方式

		// 指定饼图轮廓线的颜色
		plot.setBaseSectionOutlinePaint(Color.BLACK);
		plot.setBaseSectionPaint(Color.BLACK);

		// 设置无数据时的信息
		plot.setNoDataMessage("无对应的数据，请重新查询。");

		// 设置无数据时的信息显示颜色
		plot.setNoDataMessagePaint(Color.red);

		// 图片中显示百分比:自定义方式，{0} 表示选项， {1} 表示数值， {2} 表示所占比例 ,小数点后两位
		plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0}={1}({2})", NumberFormat.getNumberInstance(),
				new DecimalFormat("0.00%")));
		// 图例显示百分比:自定义方式， {0} 表示选项， {1} 表示数值， {2} 表示所占比例
		plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
				"{0}({2})"));

		plot.setLabelFont(new Font("隶书", Font.TRUETYPE_FONT, 12));

		// 指定图片的透明度(0.0-1.0)
		plot.setForegroundAlpha(0.65f);
		// 指定显示的饼图上圆形(false)还椭圆形(true)
		plot.setCircular(false, true);

		// 设置第一个 饼块section 的开始位置，默认是12点钟方向
		plot.setStartAngle(90);

		// 设置分饼颜色(不设置它会自己设置)
		// plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
		// plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144));

		// 把饼图生成图片
		String chartName = "";
		FileOutputStream fos_jpg = null;
		try {
			// 文件夹不存在则创建
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			chartName = path + File.separator + new Date().getTime() + ".png";
			fos_jpg = new FileOutputStream(chartName);
			// 高宽的设置影响椭圆饼图的形状
			ChartUtilities.writeChartAsPNG(fos_jpg, chart, 700, 400);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();// http://1wangxiaobo@163.com
			} catch (Exception e) {
				e.printStackTrace();
			}
			return chartName;
		}
	}

	public static String createLineChart(String title, String domainAxisName,
			String rangeAxisName, DefaultCategoryDataset ds, String path) {
		JFreeChart chart = ChartFactory.createLineChart3D(title,
				domainAxisName, rangeAxisName, ds, PlotOrientation.VERTICAL, // 绘制方向
				true, // 显示图例
				true, // 采用标准生成器
				false // 是否生成超链接
				);
		chart.getTitle().setFont((new Font("隶书", Font.CENTER_BASELINE, 20))); // 设置标题字体
		chart.getLegend().setItemFont(new Font("隶书", Font.CENTER_BASELINE, 15));// 设置图例类别字体
		// 获取绘图区对象
		CategoryPlot plot = chart.getCategoryPlot();
		plot.setBackgroundPaint(Color.LIGHT_GRAY); // 设置绘图区背景色
		plot.setRangeGridlinePaint(Color.WHITE); // 设置水平方向背景线颜色
		plot.setRangeGridlinesVisible(true);// 设置是否显示水平方向背景线,默认值为true
		plot.setDomainGridlinePaint(Color.WHITE); // 设置垂直方向背景线颜色
		plot.setDomainGridlinesVisible(true); // 设置是否显示垂直方向背景线,默认值为false

		CategoryAxis domainAxis = plot.getDomainAxis();
		domainAxis.setLabelFont(new Font("隶书", Font.CENTER_BASELINE, 15)); // 设置横轴字体
		// domainAxis.setTickLabelFont(font);// 设置坐标轴标尺值字体
		domainAxis.setLowerMargin(0.01);// 左边距 边框距离
		domainAxis.setUpperMargin(0.06);// 右边距 边框距离,防止最后边的一个数据靠近了坐标轴。
		domainAxis.setMaximumCategoryLabelLines(2);

		ValueAxis rangeAxis = plot.getRangeAxis();
		rangeAxis.setLabelFont(new Font("隶书", Font.CENTER_BASELINE, 15));
		rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());// Y轴显示整数
		rangeAxis.setAutoRangeMinimumSize(1); // 最小跨度
		rangeAxis.setUpperMargin(0.18);// 上边距,防止最大的一个数据靠近了坐标轴。
		rangeAxis.setLowerBound(0); // 最小值显示0
		rangeAxis.setAutoRange(false); // 不自动分配Y轴数据
		rangeAxis.setTickMarkStroke(new BasicStroke(1.6f)); // 设置坐标标记大小
		rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色

		// 获取折线对象
		LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot
				.getRenderer();
		BasicStroke realLine = new BasicStroke(1.8f); // 设置实线
		// 设置虚线
		float dashes[] = { 5.0f };
		BasicStroke brokenLine = new BasicStroke(2.2f, // 线条粗细
				BasicStroke.CAP_ROUND, // 端点风格
				BasicStroke.JOIN_ROUND, // 折点风格
				8f, dashes, 0.6f);

		plot.setNoDataMessage("无对应的数据，请重新查询。");
		plot.setNoDataMessagePaint(Color.RED);// 字体颜色
		// 把饼图生成图片
		String chartName = "";
		FileOutputStream fos_jpg = null;
		try {
			// 文件夹不存在则创建
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			chartName = path + File.separator + new Date().getTime() + ".png";
			fos_jpg = new FileOutputStream(chartName);
			// 高宽的设置影响椭圆饼图的形状
			ChartUtilities.writeChartAsPNG(fos_jpg, chart, 700, 400);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();// http://1wangxiaobo@163.com
			} catch (Exception e) {
				e.printStackTrace();
			}
			return chartName;
		}
	}

	public static String createCategoryDataset(String title, String domainName,
			String rangeName, DefaultCategoryDataset dataset, String path) {
		JFreeChart chart = ChartFactory
				.createBarChart(title, domainName, rangeName, dataset,
						PlotOrientation.VERTICAL, true, true, false);
		chart.getTitle().setFont((new Font("隶书", Font.CENTER_BASELINE, 20))); // 设置标题字体
		CategoryPlot plot = chart.getCategoryPlot();// 获得图表区域对象
		// 设置图表的纵轴和横轴org.jfree.chart.axis.CategoryAxis
		CategoryAxis domainAxis = plot.getDomainAxis();
		NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
		TextTitle textTitle = chart.getTitle();
		textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
		domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
		domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
		numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));

		String chartName = "";
		FileOutputStream fos_jpg = null;
		try {
			// 文件夹不存在则创建
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			chartName = path + File.separator + new Date().getTime() + ".png";
			fos_jpg = new FileOutputStream(chartName);
			// 高宽的设置影响椭圆饼图的形状
			ChartUtilities.writeChartAsPNG(fos_jpg, chart, 700, 400);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();// http://1wangxiaobo@163.com
			} catch (Exception e) {
				e.printStackTrace();
			}
			return chartName;
		}
	}

	public static String createBarChart(String title, String domainName,
			String rangeName, CategoryDataset dataset, String path) {
		JFreeChart chart = ChartFactory
				.createBarChart3D(title, domainName, rangeName, dataset,
						PlotOrientation.VERTICAL, true, true, false);
		CategoryPlot plot = chart.getCategoryPlot();
		chart.getTitle().setFont((new Font("隶书", Font.CENTER_BASELINE, 20))); // 设置标题字体
		// 设置网格背景颜色
		plot.setBackgroundPaint(Color.white);
		// 设置网络竖线颜色
		plot.setDomainGridlinePaint(Color.pink);
		// 显示每个柱的数值，并修改该数字的字体属性
		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setBaseItemLabelsVisible(true);
		// 默认的数字显示在柱子中，通过以下两句调整数字的显示
		// 注意，此句很关键，若无此句，那数字的显示会覆盖，给人数字没有显示出来的问题
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
				ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		renderer.setItemLabelAnchorOffset(10D);
		// 设置每个地区所包含的平行柱的之间的距离
		// renderer.setItemMargin(0.3);
		plot.setRenderer(renderer);

		// 设置位置
		plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
		// 将默认的左边的放到右方
		plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
		CategoryAxis domainAxis = plot.getDomainAxis();
		// 图表标题
		Font font = new Font("宋体", Font.BOLD, 16);
		chart.setTitle(title); // 标题

		// X轴乱码
		// X轴坐标上的文字：
		domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
		// X轴坐标标题
		domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
		// Y轴坐标上的文字
		numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
		// Y轴坐标标题
		numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
		// 图表底部乱码
		chart.getLegend().setItemFont(new Font("黑体", Font.PLAIN, 12));
		String chartName = "";
		FileOutputStream fos_jpg = null;
		try {
			// 文件夹不存在则创建
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			chartName = path + File.separator + new Date().getTime() + ".png";
			fos_jpg = new FileOutputStream(chartName);
			// 高宽的设置影响椭圆饼图的形状
			ChartUtilities.writeChartAsPNG(fos_jpg, chart, 800, 600);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();// http://1wangxiaobo@163.com
			} catch (Exception e) {
				e.printStackTrace();
			}
			return chartName;
		}
	}

	public static String createPointChart(XYDataset xydataset, String title,
			String domainName, String rangeName, String noDateMsg, String path) {

		JFreeChart jfreechart = ChartFactory.createScatterPlot(title,
				domainName, rangeName, xydataset, PlotOrientation.VERTICAL,
				true, false, false);
		jfreechart.setBackgroundPaint(Color.white);
		jfreechart.setBorderPaint(Color.GREEN);
		jfreechart.setBorderStroke(new BasicStroke(1.5f));
		jfreechart.getTitle().setFont(
				(new Font("隶书", Font.CENTER_BASELINE, 20)));
		jfreechart.getLegend().setItemFont(
				new Font("隶书", Font.CENTER_BASELINE, 15));// 设置图例类别字体

		XYPlot xyplot = (XYPlot) jfreechart.getPlot();
		xyplot.setNoDataMessage(noDateMsg);
		xyplot.setNoDataMessageFont(new Font("隶书", Font.BOLD, 14));
		// xyplot.setNoDataMessagePaint(new Color(87, 149, 117));

		xyplot.setBackgroundPaint(new Color(255, 253, 246));
		ValueAxis vaaxis = xyplot.getDomainAxis();
		vaaxis.setAxisLineStroke(new BasicStroke(1.5f));

		ValueAxis va = xyplot.getDomainAxis(0);
		va.setAxisLineStroke(new BasicStroke(1.5f));

		va.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细
		va.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色
		xyplot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细
		va.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色
		va.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色
		ValueAxis axis = xyplot.getRangeAxis();
		vaaxis.setLabelFont(new Font("隶书", Font.CENTER_BASELINE, 15));
		axis.setAxisLineStroke(new BasicStroke(1.5f));

		XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
				.getRenderer();
		xylineandshaperenderer.setSeriesOutlinePaint(0, Color.WHITE);
		xylineandshaperenderer.setUseOutlinePaint(true);
		NumberAxis numberaxis = (NumberAxis) xyplot.getDomainAxis();
		numberaxis.setAutoRangeIncludesZero(false);
		numberaxis.setTickMarkInsideLength(2.0F);
		numberaxis.setTickMarkOutsideLength(0.0F);
		numberaxis.setAxisLineStroke(new BasicStroke(1.5f));
		// numberaxis.setUpperBound(maxpress); 最大值设置
		// numberaxis.setLowerBound();最小值设置
		NumberAxis numberaxis1 = (NumberAxis) xyplot.getRangeAxis();
		numberaxis1.setTickMarkInsideLength(2.0F);
		numberaxis1.setTickMarkOutsideLength(0.0F);
		numberaxis1.setUpperBound(150d);
		numberaxis1.setLowerBound(35);
		numberaxis1.setAxisLineStroke(new BasicStroke(1.5f));
		numberaxis1.setLabelFont(new Font("隶书", Font.CENTER_BASELINE, 15));

		XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 89, 59); // 正常血压所在区域内边界
		XYBoxAnnotation box1 = new XYBoxAnnotation(0, 0, 119, 79);// 高血压前期所在区域内边界
		XYBoxAnnotation box2 = new XYBoxAnnotation(0, 0, 139, 89);// 高血压一期所在区域内边界
		XYBoxAnnotation box3 = new XYBoxAnnotation(0, 0, 159, 99);// 高血压二期所在区域内边界
		XYTextAnnotation text1 = new XYTextAnnotation("nomal", 70, 62.5);// 标识“正常”
		XYTextAnnotation text = new XYTextAnnotation("fore", 70, 82.5);// “高血压前期”
		XYTextAnnotation text2 = new XYTextAnnotation("one", 70, 91.5);// “高血压一期”
		XYTextAnnotation text3 = new XYTextAnnotation("two", 70, 101.5);// “高血压二期”

		// 将上面的边界线条，说明文字加入到xyplot中。
		xyplot.addAnnotation(box);
		xyplot.addAnnotation(box1);
		xyplot.addAnnotation(box2);
		xyplot.addAnnotation(box3);

		xyplot.addAnnotation(text);
		xyplot.addAnnotation(text1);
		xyplot.addAnnotation(text2);
		xyplot.addAnnotation(text3);
		String chartName = "";
		FileOutputStream fos_jpg = null;
		try {
			// 文件夹不存在则创建
			File file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
			chartName = path + File.separator + new Date().getTime() + ".png";
			fos_jpg = new FileOutputStream(chartName);
			ChartUtilities.writeChartAsPNG(fos_jpg, jfreechart, 700, 400);

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				fos_jpg.close();// http://1wangxiaobo@163.com
			} catch (Exception e) {
				e.printStackTrace();
			}
			return chartName;
		}
	}

}

/**
3D饼图：
String[] keys = { "王晓博", "李冰", "牛根生", "李开复", "马云", "其它" };
 double[] data = { 60, 60, 55, 77, 80, 50 };
 DefaultPieDataset dataset = new DefaultPieDataset();
 for (int i = 0; i < keys.length; i++) {
  dataset.setValue(keys[i], data[i]);
 }
 
 
折线图：
      DefaultCategoryDataset ds = new DefaultCategoryDataset();  
	ds.addValue(90, "温度", "2011-01-01");
	ds.addValue(100, "温度", "2011-01-02");
	ds.addValue(80, "温度", "2011-01-03");
	ds.addValue(120, "温度", "2011-01-04");
	ds.addValue(130, "湿度", "2011-01-01");
	ds.addValue(100, "湿度", "2011-01-02");
	ds.addValue(150, "湿度", "2011-01-03");
	ds.addValue(140, "湿度", "2011-01-04");
柱状图：
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
	dataset.addValue(100, "struts1.2", "张三");
	dataset.addValue(200, "struts2.x", "张三");
	dataset.addValue(300, "webwork", "张三");
	dataset.addValue(400, "spring", "张三");
	dataset.addValue(100, "struts1.2", "李四");
	dataset.addValue(200, "struts2.x", "李四");
	dataset.addValue(300, "webwork", "李四");
	dataset.addValue(400, "spring", "李四");  //dataset.addValue(-20,　"Ext　JS",　"Jan")负向坐标
散点图：
	double[][] data1 = new double[2][5];
	DefaultXYDataset xydataset = new DefaultXYDataset();  
	data1[0][0] = 80;
	data1[1][0] = 88;
	data1[0][1] = 82;
	data1[1][1] = 90;
	data1[0][2] = 98;
	data1[1][2] = 67;
	data1[0][3] = 110;
	data1[1][3] = 120;
	data1[0][4] = 122;
	data1[1][4] = 132;
	xydataset.addSeries("温度", data1); 
 
 
条形图：
	double[][] data = new double[][] { { 1310, 1220, 1110, 1000, 666 },
	{ 720, 700, 680, 640, 777 }, { 1130, 1020, 980, 800, 888 },
	{ 440, 400, 360, 300, 999 }, { 400, 400, 400, 400, 555 } };
	String[] rowKeys = { "猪肉", "牛肉", "鸡肉", "鱼肉", "羊肉" };
	String[] columnKeys = { "襄城", "樊城", "襄州", "东津", "鱼梁州" };
	CategoryDataset dataset = DatasetUtilities.createCategoryDataset(
	rowKeys, columnKeys, data);

**/